// 工具函数
const FileSaver = require("file-saver")
const XLSX = require("xlsx")
/**
 * 非空校验
*/
const _isempty = (data) => {
  if (typeof data == 'string') {
    data = data.trim()
  }

  if (data == '' || data == undefined || data == null) {
      return false
  } else {
      return true
  }
}

// 动态图片地址
const _imgPath = (url) => {
  return require(`@/assets/image/${url}`)
}

/**
 * 处理时间函数
 * status：
 *  0 => 将 xx-xx-xx xx：xx：xx 转化成 xx-xx-xx
 *  1 => 返回当月第一天和当月当日 格式为 2021-11-01 00:00:00
 *  2 => 返回当月当天 格式为 2021-11-01
 * */ 
const _time = (time, status) => {
  let now_time = new Date()
  let now_month = now_time.getMonth() + 1
  now_month = now_month < 10 ? '0' + now_month : now_month
  let now_year = now_time.getFullYear()
  let now_day = now_time.getDate()
  switch (status) {
    case 0: {
      if (time) {
        return time.substr(0, time.length - 9)
      }
      return time
    }
    case 1: {
      let start = now_year + '-' + now_month + '-01' + ' 00:00:00'
      let end = now_year + '-' + now_month + '-' + now_day + ' 23:59:59'
      return [ start, end ]
    }
    case 2: {
      let end = now_year + '-' + now_month + '-' + now_day
      return end
    }
    default: {
      return time
    }
  }
}

// 导出excell表格方法
const _excell = (tHeader, filterVal, tableData) => {
  const { export_json_to_excel } = require("@/utils/Export2Excel.js");
  const data = tableData.map(v => filterVal.map(j => v[j]));
  if (filterVal.includes('courseOrderList')) {
    data.forEach(item => {
      let a = ''
      if (item[5].length) {
        item[5].forEach(tt => {
          a += `${ tt.courseName } : ${ tt.sumQuantity };`
        })
        item[5] = a
      }
    });
  }
  export_json_to_excel(tHeader, data, "表格数据");
}

// 导出excell表格方法2.0
const _excell2 = () => {
  let wb = XLSX.utils.table_to_book(document.querySelector('#excell-table'))
  /* 获取二进制字符串作为输出 */
  var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' })
  try {
    FileSaver.saveAs( new Blob([wbout], { type: 'application/octet-stream' }), '表格数据.xlsx' )
  } catch (e) {
    if (typeof console !== 'undefined') console.log(e, wbout)
  }
  return wbout
}

// 门店类型转换
const _store = (data) => {
  data = Number(data)
  switch (data) {
    case 0: {
      return '直营'
    }
    case 1: {
      return '合营'
    }
    case 2: {
      return '支撑'
    }
    case 3: {
      return '系统使用'
    }
    default: {
      return data
    }
  }
}

module.exports = {
  _isempty,
  _imgPath,
  _time,
  _excell,
  _excell2,
  _store
}
